#include "addressbook.pb.h"
#include "pbmsg.hpp"
#include <cstdio>
#include "utils.h"

using namespace std;

int main(int argc, char* argv[])
{
    printf("创建内置的结构体，并把内置结构设置到外部结构中\n");
    {
        auto reqdata = pbmsg_t::create("../example/addressbook.proto", "AddressBook.req_data");
        int tid = 100;
        std::string name = "abc";
        reqdata->set_attr("tid", tid);
        printf("  set data.tid = %d\n", tid);
        reqdata->set_attr("name", name);
        printf("  set data.name = %s\n", name.c_str());

        auto addressbook = pbmsg_t::create("../example/addressbook.proto", "AddressBook");
        int syd = 3000;
        addressbook->set_attr("syd", syd);
        printf("  set syd = %d\n", syd);
        addressbook->set_attr("data", reqdata);


        auto person = pbmsg_t::create("../example/addressbook.proto", "Person");
        name = "kermit";
        person->set_attr("name", name);
        printf("  set new name= %s\n", name.c_str());
        std::string email = "38272702@qq.com";
        person->set_attr("email", email);
        printf("  set new email= %s\n", email.c_str());
        int id = 990;
        person->set_attr("id", id);
        printf("  set new id = %d\n", id);
        addressbook->add_attr("people", person->get_msg());

        std::string result = addressbook->get_bin();
        printf("-----------------\n  result length = %zu\n", result.length());
        auto s = dump_mem_to_str(result.data(), result.length(), 20, 7);
        printf("%s", s.c_str());
        printf("-----------------\n");

        auto person2 = pbmsg_t::create("../example/addressbook.proto", "Person");
        email = "kermit.bu@qq.com";
        person2->set_attr("email", email);
        printf("  set new email= %s\n", email.c_str());

        addressbook->set_attr("people", 0, person2->get_msg());

        result = addressbook->get_bin();
        printf("-----------------\n  result length = %zu\n", result.length());
        s = dump_mem_to_str(result.data(), result.length(), 20, 7);
        printf("%s", s.c_str());
        printf("-----------------\n");

    }
    return 0;
}